<?php
namespace app\index\controller;
use cmf\controller\HomeBaseController;
use think\Request;
use think\Db;
class PayController extends HomeBaseController
{
    public function index()
    {
      	$request = Request::instance();
      	$uid=session('userid');
      	$userinfo=Db::name('user');
      	$user=$userinfo->where('id',$uid)->find();
      	$conf=cmf_get_option("site_info");
      	if(!$user){exit('user_error');}
      	if($request->isPost()){
        	$data=Request::instance()->post();
            if($data['name']==$uid){
              	$value = cookie('pay');
              	if($value){
                	$this->error('操作频繁请一分钟后再试！',url('index/paycode'));
                }
              	$arr['order']='R'.time().mt_rand(11,99).'_'.$user['id'];
              	$arr['uid']=$user['id'];
              	$arr['addtime']=date("Y-m-d H:i:s");
              	$arr['status']=0;
              	$arr['money']=$data['money'];
              	$arr['price']=$data['money'];
              	$arr['type']='wxtype';
              	Db::name('pay')->insert($arr);
              	 $sdk=$conf['site_gwa'];
                 $userid=$arr['order'];
                 $moneys=$data['money'];
                 $sign1 = md5(floatval($moneys) . trim($userid) . $sdk);
                 $geturl = "http://pay.qcbcc.com/?sdk=" . $sdk . "&record=" . $userid . "&money=" . $moneys . "&refer=http://".$_SERVER['HTTP_HOST']."/&notify_url=http://".$_SERVER['HTTP_HOST']."/index/pay/notify&sign=" . $sign1;

                 if($moneys > 0){
                   	cookie('pay','60',40); // 指定cookie保存时间
                    exit("<meta http-equiv='Refresh' content='0;URL={$geturl}'>");
                }
            }
          
        }else{
        	$this->error('没有参数',url('index/paycode'));
        }  
    }
  	public function notify()
    {
      	$request = Request::instance();
      	$conf=cmf_get_option("site_info");
      	if($request->isPost()){
       	$data=Request::instance()->post();
       	
       	if (!function_exists('get_openid')) {
		    require $_SERVER['DOCUMENT_ROOT'].'/fastpay/Fast_Cofig.php';
		}
		
		$sign=$data['sign_notify'];//获取签名2.07以下请使用$sign=$_POST['sign'];
		$check_sign=notify_sign($data);
		if($sign!=$check_sign){
		  exit("签名失效");
		//签名计算请查看怎么计算签名,或者下载我们的SDK查看
		}
		
		$uid         = $data['uid'];//支付用户
		$total_fee   = $data['total_fee'];//支付金额
		$pay_title   = $data['pay_title'];//标题
		$sign        = $data['sign'];//签名
		$order_no    = $data['order_no'];//订单号
		$me_pri      = $data['me_pri'];//我们网站生成的金额,参与签名的,跟实际金额有差异
           if(1==1){
			 $uid=explode("_",$order_no);
             $up=Db::name('pay')->where('order', $order_no)->update(['status' => 1]);
             if($up){
             	Db::name('user')->where('id', $uid[2])->setInc('balance', $total_fee);
             	echo "success";
             }
            }else{
                echo 'keyerror';
            }
        }
    }

  	public function with()
    {
        $request = Request::instance();
      	$uid=session('userid');
      	$userinfo=Db::name('user');
      	$user=$userinfo->where('id',$uid)->find();
      	$conf=cmf_get_option("site_info");
      	if(!$user){exit('user_error');}
      	if($request->isPost()){
            $data=Request::instance()->post();
            if($data['uid']==$uid){
              	$where=array(
                	'uid'=>$uid,
                  	'status'=>1
                );

              	if($data['money']>=$conf['txmax']){ $this->error('提现通道繁忙请20秒后再试',url('index/with'));} 
              
               if($user['balance']<10){ $this->error('最少提现10元哟',url('index/with'));}
              
               if($data['money']<$conf['site_admin_txjine']){
                 $this->error('最低提现'.$conf['site_admin_txjine'].'元',url('index/with'));
               }
               
               if($data['money']>$user['balance']){
               	 $this->error('对不起你的余额不足',url('index/with'));
               }
                
               $sumzjin=Db::name('withdraw')->where(['uid'=>$uid,'status'=>1])->whereTime('addtime', 'd')->select();
               $zongtix=count($sumzjin);
              
               if($zongtix>5){
                  $this->error('当前每日提现次数为5次，请0点后再试，谢谢！',url('index/with'));
                  exit;
                }
              
               //减去用户提现金额
               Db::name('user')->where('id', $user['id'])->setDec('balance',$data['money']);
               $ddh='T'.time().mt_rand(11,99).'_'.$user['id'];
               $arr['order']=$ddh;
               $arr['uid']=$user['id'];
               $arr['addtime']=date("Y-m-d H:i:s");
               $arr['status']=0;
               $arr['money']=$data['money'];
               $arr['type']=1;
                Db::name('withdraw')->insert($arr);
              	
              	$txmoney=$data['money']*0.98;
              	$txmoney=round($txmoney,2);
              	$key = "asedr";
                $u = time();
                $post_data = array (
                    'mid' => 943, //在掌上零钱里面获取的uid
                    'jine' => $txmoney, //要请求发放的金额
                    'openid'=>$user['txopen'], //第二步获取的openid
                    'tixianid'=>$u, //本地的提现id【要求唯一】字符串类型的数字，最大长度11位数
                    'lailu' =>"会员ID: ".$user['id'], //可选参数
                );
                $mkey = md5($post_data['mid'].$post_data['jine'].$post_data['openid'].$key);
                $post_data['mkey'] = $mkey;
                $post_data['lx'] = 999;//保持默认
                $url ='http://jfcms12.com/jieru.php';
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                // post数据
                curl_setopt($ch, CURLOPT_POST, 1);
                // post的变量
                curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
                $output = curl_exec($ch);
                curl_close($ch);
                //打印获得的数据
                $json = json_decode($output,true);
              	if($json['o']=='yes'){
                	Db::name('withdraw')->where('order', $ddh)->update(['status' => 1]);
                  	$this->error('提现成功',url('index/with'));
                }else{
                  	Db::name('user')->where('id', $user['id'])->setInc('balance',$data['money']);
                	$this->error($json['msg'],url('index/with'));
                }
            }  
        }else{
        	$this->error('没有参数',url('index/with'));
        }  
    }
  	public function yjtixian()
    {
        $request = Request::instance();
      	$uid=session('userid');
      	$userinfo=Db::name('user');
      	$user=$userinfo->where('id',$uid)->find();
      	$conf=cmf_get_option("site_info");
      	if(!$user){exit('user_error');}
      	if($request->isPost()){
            $data=Request::instance()->post();
            if($data['uid']==$uid){
                $data['money']=intval($data['money']);
               if($data['money']>=$conf['txmax']){ $this->error('提现通道繁忙请20秒后再试',url('index/with'));} 
              
               if($user['score']<1){ $this->error('最低提现1元哟',url('index/daili'));}
              
               if($data['money']<1){
                 $this->error('最低提现1元',url('index/daili'));
               }
               
               if($user['score']<$data['money']){
               	 $this->error('对不起你的佣金不足',url('index/with'));
               }
               //减去用户提现金额
               Db::name('user')->where('id', $user['id'])->setDec('score',$data['money']);
               $ddh='T'.time().mt_rand(11,99).'_'.$user['id'];
               $arr['order']=$ddh;
               $arr['uid']=$user['id'];
               $arr['addtime']=date("Y-m-d H:i:s");
               $arr['status']=0;
               $arr['money']=$data['money'];
               $arr['type']=2;
                Db::name('withdraw')->insert($arr);
               	$txmoney=$data['money'];
              	$key = "asedr";
                $u = time();
                $post_data = array (
                    'mid' => 943, //在掌上零钱里面获取的uid
                    'jine' => $txmoney, //要请求发放的金额
                    'openid'=>$user['txopen'], //第二步获取的openid
                    'tixianid'=>$u, //本地的提现id【要求唯一】字符串类型的数字，最大长度11位数
                    'lailu' =>"会员ID: ".$user['id'], //可选参数
                );
                $mkey = md5($post_data['mid'].$post_data['jine'].$post_data['openid'].$key);
                $post_data['mkey'] = $mkey;
                $post_data['lx'] = 999;//保持默认
                $url ='http://jfcms12.com/jieru.php';
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                // post数据
                curl_setopt($ch, CURLOPT_POST, 1);
                // post的变量
                curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
                $output = curl_exec($ch);
                curl_close($ch);
                //打印获得的数据
                $json = json_decode($output,true);
              	if($json['o']=='yes'){
                	Db::name('withdraw')->where('order', $ddh)->update(['status' => 1]);
                  	$this->error('提现成功',url('index/with'));
                }else{
                  	Db::name('user')->where('id', $user['id'])->setInc('balance',$txmoney);
                	$this->error($json['msg'],url('index/with'));
                }
            }  
        }else{
        	$this->error('没有参数',url('index/with'));
        }  
    }
}
